<template>
  <div class="login_container">
      <el-row>
        <el-col :span="12" :xs="0"></el-col>
        <el-col :span="12" :xs="24">
          <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginForms">
            <h1>Hello</h1>
            <h2>欢迎来到商通智控运营平台</h2>
            <el-form-item prop="username">
              <el-input :prefix-icon="User" v-model="loginForm.username"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input type="password"  v-model="loginForm.password" :prefix-icon="Lock" show-password></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="default" class="login_btn" @click="login" :loading="loading">登录</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
  </div>
</template>

<script setup lang="ts">
import {User,Lock} from '@element-plus/icons-vue'
import {reactive,ref} from 'vue'
import useUserStore from '@/store/modules/user';
import { useRouter,useRoute} from 'vue-router';
import { ElNotification } from 'element-plus';
import {getTime} from '@/utils/time'
let $router = useRouter()
let $route = useRoute()
let useStore = useUserStore()
let loginForm = reactive({username:'admin',password:'111111'})
let loading = ref(false)
let loginForms = ref()
const login =async ()=>{
  loading.value = true
  //等校验通过才能发请求
  await loginForms.value.validate()
  try {
    //保证登录成功
   await useStore.userLogin(loginForm)
  //  vc拿到query参数，如果没有query参数往首页调，如果有query参数就往query参数调
  let redirect:any = $route.query.redirect
    $router.push({path:redirect||'/'})
    ElNotification({
      type:'success',
      message:'欢迎回来',
      title:`哈喽，${getTime()}好`
    })
    loading.value = false
  } catch (error) {
    loading.value = false
    ElNotification({
      type:'error',
      message:(error as Error).message
    })
  }
}
// @ts-ignore 
const validatorUserName = (rule: any, value: any, callback: any)=>{
  //rule即为校验规则对象
  //value即为表单内容
  //callback即为符合条件调用，不符合条件的话注入错误信息
  if(value.length>=5){
    callback();
  }else{
    callback(new Error('账号长度至少为五位'));
  }
}
// @ts-ignore 
const validatorPassWord = (rule: any, value: any, callback: any)=>{
  if(value.length>=6){
    callback()
  }else{
    callback(new Error('密码长度至少为六位'))
  }
}
//定义表单校验需要配置对象
const rules = {
  username:[
    // 触发的时机：失去焦点的时候
    // {required:true,min:5, max:10, message:'账号长度至少为5位',trigger:'change'},
    {trigger:'change',validator:validatorUserName}
  ],
  password:[
    // {required:true,min:6, max:15, message:'密码长度至少为6位',trigger:'change'}
    {trigger:'change',validator:validatorPassWord}
  ]
}


</script>

<style scoped>
.login_container{
  width:100%;
  height:100vh;
  background: url('../../assets/images/background.jpg') no-repeat;
  background-size: cover
}
.login_form {
  /* 占右侧的80% */
  width:80%;
  /* 距上边的距离，视口宽度的30% */
  top:30vh;
  position: relative;
  background:url("@/assets/images/login_form.png") no-repeat;
  background-size: cover;
  padding: 40px;
}
h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }
    .login_btn {
      width: 100%;
    }
</style>